﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace EPE.Common
{
    public class InputChecker
    {

        #region"DBNULL检测"
        /// <summary>
        /// DBNULL检测
        /// </summary>
        /// <param name="argValue"></param>
        /// <returns></returns>
        public static bool IsDBNull(object argValue)
        {
            if (argValue == DBNull.Value)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region "数值型检测"
        /// <summary>
        /// 数值型检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckNumber(string strParam)
        {
            //Regex regex1 = new Regex("^[0-9]+$");
            Regex regex1 = new Regex("^[\\+\\-]?[0-9]*\\.?[0-9]+$");
            if (!regex1.IsMatch(strParam))
            {
                return false;
            }
            return true;
        }
        #endregion integer

        #region"整数检测"
        /// <summary>
        /// 整数检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckInteger(string strParam)
        {
            Regex regex1 = new Regex("^[0-9]+$");
            if (!regex1.IsMatch(strParam))
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "长度检测"
        /// <summary>
        /// 长度检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool CheckLength(string strParam, int length)
        {
            if (strParam.Length != length)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "最大长度检测"
        /// <summary>
        /// 最大长度检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool CheckMaxLength(string strParam, int length)
        {
            if (strParam.Length > length)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "日期大小对比"
        /// <summary>
        /// 日期大小对比
        /// </summary>
        /// <param name="dateParam"></param>
        /// <param name="date2Param"></param>
        /// <returns></returns>
        public static bool CheckDate(DateTime dateParam, DateTime date2Param)
        {
            if (dateParam > date2Param)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "日期大小对比，是否小于今天"
        /// <summary>
        /// 日期大小对比，是否小于今天
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckSmallToday(DateTime strParam)
        {
            if (strParam < DateTime.Today)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "月检测"
        /// <summary>
        /// 月检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckMonth(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                int num1 = int.Parse(strParam);
                return ((1 <= num1) && (num1 <= 12));
            }
            return false;
        }
        #endregion

        #region "日检测"
        /// <summary>
        /// 日检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckDay(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                int num1 = int.Parse(strParam);
                return ((1 <= num1) && (num1 <= 0x1f));
            }
            return false;
        }
        #endregion

        #region "邮箱检测"
        /// <summary>
        /// 邮箱检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckEmail(string strParam)
        {
            string text1 = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            //			string text1 = @"^([_a-z0-9.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex regex1 = new Regex(text1);
            return regex1.IsMatch(strParam);
        }
        #endregion

        #region "带小数点数值型检测"
        /// <summary>
        /// 带小数点数值型检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckDecimal(string strParam)
        {
            Regex regex1 = new Regex(@"^[0-9\.]+$");
            return regex1.IsMatch(strParam);
        }
        #endregion

        #region "秒数检测"
        /// <summary>
        /// 秒数检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckMinute(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                return (int.Parse(strParam) <= 0x3b);
            }
            return false;
        }
        #endregion

        #region "金额类型检测"
        /// <summary>
        /// 金额类型检测
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckMoney(string str)
        {
            //Regex regex1 = new Regex("(^(([0-9]{1,3},)([0-9]{3},)*[0-9]{3}$))|(^[0-9]+$)");
            //Regex regex1 = new Regex(@"^[0-9][0-9]{0,2}(,{0,1}[0-9]{3})(\.[0-9]{0,2})$");
            Regex regex1 = new Regex(@"^(([0])|([1-9]\d{0,23}))(\.\d{1,2})?$");
            //Regex regex1 = new Regex("(^(([0-9]{1,3},)([0-9]{3},)*[0-9]{3}$))|(^[0-9]+$)*(\\.[0-9]{0,2})$");
            //return (regex1.IsMatch(str) && (((str.Substring(0, 1)) != "0") || ((str.Substring(0,1).ToString() == "0") && (str.Length == 1))));
            return (regex1.IsMatch(str));
        }
        #endregion

        #region"电话检测"
        /// <summary>
        /// 电话检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckTelphone(string strParam)
        {
            //string text1 = @"^\d{1,5}-\d{1,5}-\d{1,5}$";
            string text1 = @"^\d{1,4}-\d{1,8}$";
            Regex regex1 = new Regex(text1);
            return (regex1.IsMatch(strParam) && (strParam.Length <= 13));
        }
        #endregion

        #region"手机检测"
        /// <summary>
        /// 手机检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckMobile(string strParam)
        {
            string text1 = @"^\d{1,13}$";
            Regex regex1 = new Regex(text1);
            return (regex1.IsMatch(strParam) && (strParam.Length <= 17));
        }
        #endregion

        #region "小时时间型检测"
        /// <summary>
        /// 小时时间型检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckTime(string strParam)
        {
            Regex regex1 = new Regex("(^[0-9]{4}$)|(^[0-9]{1,2}:[0-9]{1,2}$)|(^[0-9]{6}$)|(^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$)");
            if (regex1.IsMatch(strParam))
            {
                int num1 = 0;
                int num2 = 0;
                int num3 = 0;
                bool flag1 = false;
                if (strParam.IndexOf(":") == -1)
                {
                    num1 = int.Parse(strParam.Substring(0, 2));
                    num2 = int.Parse(strParam.Substring(2, 2));
                    if (strParam.Length == 6)
                    {
                        flag1 = true;
                        num3 = int.Parse(strParam.Substring(4, 2));
                    }
                }
                else
                {
                    string[] textArray1 = strParam.Split(new char[] { ':' });
                    num1 = int.Parse(textArray1[0]);
                    num2 = int.Parse(textArray1[1]);
                    if (textArray1.Length == 3)
                    {
                        flag1 = true;
                        num3 = int.Parse(textArray1[2]);
                    }
                }
                return (flag1 ? (((num1 <= 0x17) && (num2 <= 0x3b)) && (num3 <= 0x3b)) : ((num1 <= 0x17) && (num2 <= 0x3b)));
            }
            return false;
        }
        #endregion

        #region "网址检测"
        /// <summary>
        /// 网址检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckURL(string strParam)
        {
            string text1 = @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?";
            Regex regex1 = new Regex(text1);
            return regex1.IsMatch(strParam.ToLower());
        }
        #endregion

        #region "本地网址检测"
        /// <summary>
        /// 本地网址检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckLocalhostURL(string strParam)
        {
            string text1 = @"^([^\\^\/]+[\\\/]+|\\\\[^\\]+)(.*)$";
            Regex regex1 = new Regex(text1);
            return regex1.IsMatch(strParam.ToLower());
        }
        #endregion

        #region "最大年(9999)比较检测"
        /// <summary>
        /// 年检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckYear(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                return (int.Parse(strParam) <= 0x270f);
            }
            return false;
        }
        #endregion

        #region "最小最大年(1900-9999)检测"
        /// <summary>
        /// 最大年检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckMaxMinYear(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                return (int.Parse(strParam) >= 0x076c && int.Parse(strParam) <= 0x270f);
            }
            return false;
        }
        #endregion

        #region "年月类型检测"
        /// <summary>
        /// 年月检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckYearMonth(string strParam)
        {
            Regex regex1 = new Regex("(^[0-9]{4}[0-9]{2}$)|(^[0-9]{1,4}/[0-9]{1,2}$)");
            if (regex1.IsMatch(strParam))
            {
                int num1;
                int num2;
                if (strParam.IndexOf("/") == -1)
                {
                    num1 = int.Parse(strParam.Substring(0, 4));
                    num2 = int.Parse(strParam.Substring(4, 2));
                }
                else
                {
                    string[] textArray1 = strParam.Split(new char[] { '/' });
                    num1 = int.Parse(textArray1[0]);
                    num2 = int.Parse(textArray1[1]);
                }
                return (((num1 <= 0x270f) && (num2 <= 12)) && (num2 != 0));
            }
            return false;
        }
        #endregion

        #region "小时类型检测"
        /// <summary>
        /// 小时检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckHour(string strParam)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                int num1 = int.Parse(strParam);
                return ((0 < num1) && (num1 <= 0x17));
            }
            return false;
        }
        #endregion

        #region "日期型检测"
        /// <summary>
        /// 日期型检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckDateTime(string strParam)
        {
            try
            {
                DateTime temp = DateTime.Parse(strParam);

                string[] strSub;
                strSub = strParam.Split('/');
                if (strSub.Length > 0)
                {
                    for (int i = 0; i < strSub.Length; i++)
                    {
                        if (strSub[i].Equals(""))
                        {
                            return false;
                        }
                    }
                }

                string strYear;
                string strMonth;
                string strDay;

                strYear = temp.Year.ToString();
                strMonth = temp.Month.ToString();
                strDay = temp.Day.ToString();


                if (CheckMaxMinYear(strYear) && CheckMonth(strMonth) && CheckDay(strDay))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "日期(年月日)检测"
        /// <summary>
        /// 日期检测
        /// </summary>
        /// <param name="yearParam"></param>
        /// <param name="monthParam"></param>
        /// <param name="dayParam"></param>
        /// <returns></returns>
        public static bool CheckDateTime(string yearParam, string monthParam, string dayParam)
        {
            try
            {
                new DateTime(int.Parse(yearParam), int.Parse(monthParam), int.Parse(dayParam));
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion

        #region "出生日期检测"
        /// <summary>
        /// 出生日期检测
        /// </summary>
        /// <param name="argParam"></param>
        /// <returns></returns>
        public static bool CheckBirthday(string argParam)
        {
            try
            {
                int iYear = Convert.ToInt32(argParam.Substring(0, 4));
                int iMonth = Convert.ToInt32(argParam.Substring(4, 2));
                int iDay = Convert.ToInt32(argParam.Substring(6, 2));

                new DateTime(iYear, iMonth, iDay);
                //new DateTime(long.Parse(argParam));
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion

        #region"日期对比可判断大于小于等于"
        /// <summary>
        /// 日期对比
        /// </summary>
        /// <param name="fromYear"></param>
        /// <param name="fromMonth"></param>
        /// <param name="fromDay"></param>
        /// <param name="toYear"></param>
        /// <param name="toMonth"></param>
        /// <param name="toDay"></param>
        /// <returns></returns>
        public static int CheckDateTimeCompare(string fromYear, string fromMonth, string fromDay, string toYear, string toMonth, string toDay)
        {
            if (((fromYear.Equals(string.Empty) && fromMonth.Equals(string.Empty)) && (fromDay.Equals(string.Empty) && toYear.Equals(string.Empty))) && (toMonth.Equals(string.Empty) && toDay.Equals(string.Empty)))
            {
                return 0;
            }
            if (((fromYear.Equals(string.Empty) && fromMonth.Equals(string.Empty)) && fromDay.Equals(string.Empty)) || ((toYear.Equals(string.Empty) && toMonth.Equals(string.Empty)) && toDay.Equals(string.Empty)))
            {
                return 1;
            }
            DateTime time1 = new DateTime(int.Parse(fromYear), int.Parse(fromMonth), int.Parse(fromDay));
            DateTime time2 = new DateTime(int.Parse(toYear), int.Parse(toMonth), int.Parse(toDay));
            if (time1 < time2)
            {
                return 1;
            }
            if (time1 == time2)
            {
                return 0;
            }
            return -1;
        }
        #endregion

        #region"日期对比可判断大于小于等于"
        /// <summary>
        /// 日期对比
        /// </summary>
        /// <param name="fromDay"></param>
        /// <param name="toDay"></param>
        /// <returns></returns>
        public static int CheckDateTimeCompare(DateTime fromDay, DateTime toDay)
        {
            return CheckDateTimeCompare(fromDay.Year.ToString(), fromDay.Month.ToString(), fromDay.Day.ToString(), toDay.Year.ToString(), toDay.Month.ToString(), toDay.Day.ToString());
        }
        #endregion

        #region"双字节检测"
        /// <summary>
        /// 双字节检测
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDubbleStr(string value)
        {
            byte[] bytStr = System.Text.Encoding.Default.GetBytes(value);
            if (!((bytStr.Length == (value.Length * 2))))
            {
                return false;
            }
            return true;
        }
        #endregion

        #region"不带特殊符号半角英数"
        /// <summary>
        /// 半角英数
        /// </summary>
        /// <param name="myStr"></param>
        /// <returns></returns>
        public static bool AlpChk(string myStr)
        {
            int i = 0;
            char[] chars = myStr.ToCharArray();
            while (i < myStr.Length)
            {
                char charData = chars[i];
                if (i == 0)
                {
                    if ((charData == '-') || (charData == '_') || (charData == '@') || (charData == '.'))
                    {
                        return false;
                    }
                }
                if (((charData < 'A') || (charData > 'Z')) && ((charData < 'a') || (charData > 'z')) && ((charData < '0') || (charData > '9')) && (charData != '-') && (charData != '_') && (charData != '@') && (charData != '.'))
                {
                    return false;
                }
                i = i + 1;
            }
            return true;
        }
        #endregion

        #region"带特殊符号半角英数字"
        /// <summary>
        /// 带特殊符号半角英数字
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckAlpAndNum(string strParam)
        {
            Regex regex1 = new Regex("^[A-Za-z0-9]+$");
            return regex1.IsMatch(strParam);
        }
        #endregion

        #region"日本文字长度检测"
        /// <summary>
        /// 日本文字长度检测
        /// </summary>
        /// <param name="strParam"></param>
        /// <param name="lengthParam"></param>
        /// <returns></returns>
        public static bool CheckByteLengthLEq(string strParam, int lengthParam)
        {
            int num1 = Encoding.GetEncoding("Shift_JIS").GetByteCount(strParam);
            return (num1 <= lengthParam);
        }
        #endregion

        #region"日文最大最小长度"
        /// <summary>
        /// 日文最大最小长度
        /// </summary>
        /// <param name="strParam"></param>
        /// <param name="minLengthParam"></param>
        /// <param name="maxLengthParam"></param>
        /// <returns></returns>
        public static bool CheckByteLengthSpan(string strParam, int minLengthParam, int maxLengthParam)
        {
            int num1 = Encoding.GetEncoding("Shift_JIS").GetByteCount(strParam);
            return ((minLengthParam <= num1) && (num1 <= maxLengthParam));
        }
        #endregion

        #region"邮政编码"
        /// <summary>
        /// 邮政编码
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckPostalCode(string strParam)
        {
            //			Regex regex1 = new Regex("(^[0-9]{3}-?[0-9]{4}$)|(^[0-9]{3}-?[0-9]{2}$)");
            Regex regex1 = new Regex("^[0-9]{6,8}$");
            return regex1.IsMatch(strParam);
        }
        #endregion

        #region"日文半角"
        /// <summary>
        /// 日文半角
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ValidateHankakuKana(string value)
        {
            string hankakuKana = "";

            char[] chars = value.ToCharArray();
            char[] kanas = hankakuKana.ToCharArray();
            int i = 0;
            while (i < value.Length)
            {
                char c = chars[i];
                bool ret = false;
                int j = 0;
                while (j < hankakuKana.Length)
                {
                    char k = kanas[j];
                    if (c == k)
                    {
                        ret = true;
                        break;
                    }
                    j = j + 1;
                }
                if (!(ret))
                {
                    return false;
                }
                i = i + 1;
            }
            return true;
        }
        #endregion

        #region"日文全角字符"
        /// <summary>
        /// 日文全角字符
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsZenkakuKana(char c)
        {
            return ((c > 0x30a0 && c <= 0x30ff) || (c == 0x3000));
        }
        #endregion

        #region"日文全角字符串"
        /// <summary>
        /// 日文全角字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ValidateZenkakuKana(string value)
        {
            char[] chars = value.ToCharArray();
            int i = 0;
            while (i < value.Length)
            {
                if (!(IsZenkakuKana(chars[i])))
                {
                    return false;
                }
                i = i + 1;
            }
            return true;
        }
        #endregion

        #region "带时间日期比较可判断大于小于等于"
        /// <summary>
        /// 日期比较
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static int ComDateTime(DateTime strParam1, DateTime strParam2)
        {
            if (strParam1 > strParam2)
            {
                return 1;
            }
            else if (strParam1 == strParam2)
            {
                return 0;
            }
            else
            {
                return -1;
            }

        }
        #endregion

        #region "指定大小的数值比较"
        /// <summary>
        /// 指定大小的数字比较
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckMaxNumber(string strParam, int argMax)
        {
            if (InputChecker.CheckNumber(strParam))
            {
                return (double.Parse(strParam) <= argMax);
            }
            return false;
        }
        #endregion

        #region "判断身份证是否合法"
        /// <summary>
        /// 判断身份证是否合法
        /// </summary>
        /// <param name="str">身份证号码</param>
        /// <returns>bool</returns>
        public static bool CheckIDCard(string str)
        {
            // 设置正则表达式  
            string zzbds = @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$";

            // 判断并得到结果
            Match m = Regex.Match(str, zzbds);

            if (m.Success)
            {
                string strBirthday = string.Empty;
                if (str.Length == 15)
                {
                    strBirthday = "19" + str.Substring(6, 6);
                }
                else
                {
                    strBirthday = str.Substring(6, 8);
                }
                //加上出生年的限制
                if (Convert.ToInt32(strBirthday.Substring(0, 4)) < 1950)
                {
                    return false;
                }

                return CheckBirthday(strBirthday);
            }


            return m.Success;


        }
        #endregion



        #region"保留小数点后几位"
        /// <summary>
        /// 保留小数点后几位
        /// </summary>
        /// <param name="strParam"></param>
        /// <returns></returns>
        public static bool CheckDecimalPoint(string strParam, int argLength)
        {
            int iLastIndex = strParam.LastIndexOf(".");
            string strSubstring = string.Empty;
            if (iLastIndex >= 0)
            {
                strSubstring = strParam.Substring(iLastIndex + 1, strParam.Length - iLastIndex - 1);
            }
            int iSubstring;
            iSubstring = strSubstring.Length;

            return (iSubstring <= argLength);
        }
        #endregion
    }
}
